package cn.stylefeng.guns.modular.school.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.school.entity.ClassSchoolTable;
import cn.stylefeng.guns.modular.school.entity.CourseSign;
import cn.stylefeng.guns.modular.school.entity.StudentInfo;
import cn.stylefeng.guns.modular.school.entity.TeacherInfo;
import cn.stylefeng.guns.modular.school.mapper.ClassSchoolTableMapper;
import cn.stylefeng.guns.modular.school.model.params.ClassSchoolTableParam;
import cn.stylefeng.guns.modular.school.model.result.ClassSchoolTableResult;
import cn.stylefeng.guns.modular.school.service.ClassSchoolTableService;
import cn.stylefeng.guns.modular.school.service.CourseSignService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 班级课表 服务实现类
 * </p>
 *
 * @author
 * @since 2020-05-02
 */
@Service
public class ClassSchoolTableServiceImpl extends ServiceImpl<ClassSchoolTableMapper, ClassSchoolTable> implements ClassSchoolTableService {
    @Autowired
    private CourseSignService courseSignService;

    @Override
    public void add(ClassSchoolTableParam param) {
        ClassSchoolTable entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ClassSchoolTableParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(ClassSchoolTableParam param) {
        ClassSchoolTable oldEntity = getOldEntity(param);
        ClassSchoolTable newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ClassSchoolTableResult findBySpec(ClassSchoolTableParam param) {
        return null;
    }

    @Override
    public List<ClassSchoolTableResult> findListBySpec(ClassSchoolTableParam param) {
        return null;
    }

    @Override
    public List<ClassSchoolTableResult> findListByClassId(ClassSchoolTableParam param) {
        return this.baseMapper.findListByClassId(param);
    }

    @Override
    public Map findStudentSchoolTable(ClassSchoolTableParam param) {
        Map map = new HashMap();
        // 取课表
        //ClassSchoolTableParam param = new ClassSchoolTableParam();
        Integer classId = param.getClassId();
        StudentInfo studentInfo = param.getStudentInfo();
        param.setType("1");
        param.setClassId(classId);
        List<ClassSchoolTableResult> classSchoolTableResultList = baseMapper.findListByClassId(param);
        getSign(studentInfo, classSchoolTableResultList);
        map.put("monday", classSchoolTableResultList);

        param = new ClassSchoolTableParam();
        param.setType("2");
        param.setClassId(classId);
        classSchoolTableResultList = baseMapper.findListByClassId(param);
        getSign(studentInfo, classSchoolTableResultList);
        map.put("tuesday", classSchoolTableResultList);

        param = new ClassSchoolTableParam();
        param.setType("3");
        param.setClassId(classId);
        classSchoolTableResultList = baseMapper.findListByClassId(param);
        getSign(studentInfo, classSchoolTableResultList);
        map.put("wednesday", classSchoolTableResultList);

        param = new ClassSchoolTableParam();
        param.setType("4");
        param.setClassId(classId);
        classSchoolTableResultList = baseMapper.findListByClassId(param);
        getSign(studentInfo, classSchoolTableResultList);
        map.put("thursday", classSchoolTableResultList);

        param = new ClassSchoolTableParam();
        param.setType("5");
        param.setClassId(classId);
        classSchoolTableResultList = baseMapper.findListByClassId(param);
        getSign(studentInfo, classSchoolTableResultList);
        map.put("friday", classSchoolTableResultList);

        param = new ClassSchoolTableParam();
        param.setType("6");
        param.setClassId(classId);
        classSchoolTableResultList = baseMapper.findListByClassId(param);
        getSign(studentInfo, classSchoolTableResultList);
        map.put("saturday", classSchoolTableResultList);

        param = new ClassSchoolTableParam();
        param.setType("7");
        param.setClassId(classId);
        classSchoolTableResultList = baseMapper.findListByClassId(param);
        getSign(studentInfo, classSchoolTableResultList);
        map.put("sunday", classSchoolTableResultList);
        return map;
    }

    @Override
    public Map findTeacherSchoolTable(ClassSchoolTableParam param) {
        Map map = new HashMap();
        // 取课表
        //ClassSchoolTableParam param = new ClassSchoolTableParam();
        param.setType("1");
        List<ClassSchoolTableResult> classSchoolTableResultList = baseMapper.findListByTeacherId(param);
        getSign(param.getTeacherId(), classSchoolTableResultList);
        map.put("monday", classSchoolTableResultList);

        param.setType("2");
        classSchoolTableResultList = baseMapper.findListByTeacherId(param);
        getSign(param.getTeacherId(), classSchoolTableResultList);
        map.put("tuesday", classSchoolTableResultList);

        param.setType("3");
        classSchoolTableResultList = baseMapper.findListByTeacherId(param);
        getSign(param.getTeacherId(), classSchoolTableResultList);
        map.put("wednesday", classSchoolTableResultList);

        param.setType("4");
        classSchoolTableResultList = baseMapper.findListByTeacherId(param);
        getSign(param.getTeacherId(), classSchoolTableResultList);
        map.put("thursday", classSchoolTableResultList);

        param.setType("5");
        classSchoolTableResultList = baseMapper.findListByTeacherId(param);
        getSign(param.getTeacherId(), classSchoolTableResultList);
        map.put("friday", classSchoolTableResultList);

        param.setType("6");
        classSchoolTableResultList = baseMapper.findListByTeacherId(param);
        getSign(param.getTeacherId(), classSchoolTableResultList);
        map.put("saturday", classSchoolTableResultList);

        param.setType("7");
        classSchoolTableResultList = baseMapper.findListByTeacherId(param);
        getSign(param.getTeacherId(), classSchoolTableResultList);
        map.put("sunday", classSchoolTableResultList);

        return map;
    }

    private void getSign(Integer teacherId, List<ClassSchoolTableResult> classSchoolTableResultList) {
        classSchoolTableResultList.forEach(classSchoolTableResult -> {
            List<CourseSign> courseSigns = courseSignService.list(new QueryWrapper<CourseSign>().eq("sign_date", DateUtil.today()).eq("student_teacher_id", teacherId).eq("type", 2).eq("school_table_id", classSchoolTableResult.getId()));
            if (courseSigns.size() > 0) {
                classSchoolTableResult.setSigned(true);
            }
            classSchoolTableResult.setTeacher(true);
        });
    }


    private void getSign(StudentInfo studentInfo, List<ClassSchoolTableResult> classSchoolTableResultList) {
        classSchoolTableResultList.forEach(classSchoolTableResult -> {
            CourseSign courseSign = courseSignService.getOne(new QueryWrapper<CourseSign>().eq("sign_date", DateUtil.today()).eq("student_teacher_id", studentInfo.getId()).eq("type", 1).eq("school_table_id", classSchoolTableResult.getId()));
            if (courseSign != null) {
                classSchoolTableResult.setSigned(true);
                if ("2".equals(courseSign.getSignOrLeave())) {
                    classSchoolTableResult.setLeave(true);
                }
            }
        });
    }

    @Override
    public LayuiPageInfo findPageBySpec(ClassSchoolTableParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ClassSchoolTableParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ClassSchoolTable getOldEntity(ClassSchoolTableParam param) {
        return this.getById(getKey(param));
    }

    private ClassSchoolTable getEntity(ClassSchoolTableParam param) {
        ClassSchoolTable entity = new ClassSchoolTable();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
